using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.XPath;
using System.Xml.Xsl;

namespace BadHabit.Factory.Reports {



    public class HourBankReport : Report {

        private static Data.ProjectDataSetTableAdapters.BacklogItemTableAdapter backlogItemTA = new BadHabit.Factory.Data.ProjectDataSetTableAdapters.BacklogItemTableAdapter();
        private static Data.ProjectDataSetTableAdapters.BacklogItemUnitTableAdapter backlogItemUnitTA = new BadHabit.Factory.Data.ProjectDataSetTableAdapters.BacklogItemUnitTableAdapter();
        private static Data.ProjectDataSetTableAdapters.BacklogItemHoursTableAdapter backlogItemHoursTA = new BadHabit.Factory.Data.ProjectDataSetTableAdapters.BacklogItemHoursTableAdapter();

        private static Data.ProjectDataSetTableAdapters.ProjectTableAdapter projectTA = new BadHabit.Factory.Data.ProjectDataSetTableAdapters.ProjectTableAdapter();
        
        private static Data.ProjectHourBankDataSetTableAdapters.ProjectHourBankChildProjectsTableAdapter reportChildTA = new BadHabit.Factory.Data.ProjectHourBankDataSetTableAdapters.ProjectHourBankChildProjectsTableAdapter();


        private static Data.ProjectHourBankDataSetTableAdapters.PayableTasksTableAdapter payableTasksTA = new BadHabit.Factory.Data.ProjectHourBankDataSetTableAdapters.PayableTasksTableAdapter();


        private Data.ProjectDataSetTableAdapters.SprintTableAdapter sprintTA = new BadHabit.Factory.Data.ProjectDataSetTableAdapters.SprintTableAdapter();


        /// <summary>
        /// Implements a Report class to generate a backlog report.
        /// </summary>
        /// <param name="factory"></param>
        public HourBankReport(Factory factory) : base(factory) { }


        /// <summary>
        /// The template type name.
        /// </summary>
        public override string TemplateTypeName {
            get {
                return "HourBankReport";
            }
        }

        public override System.Data.DataSet CreateDataSet() {
            Data.ProjectHourBankDataSet report = new BadHabit.Factory.Data.ProjectHourBankDataSet();
            report.EnforceConstraints = false;

            payableTasksTA.FillByProjectUId(report.PayableTasks, ProjectUId);



            Data.ProjectDataSet project = factory.RetriveProjectWithOutProposal(ProjectUId);
            project.EnforceConstraints = false;

            /*
            backlogItemTA.FillUsedBankByProjectUId(project.BacklogItem, projectUId);
            backlogItemUnitTA.FillByProjectUId(project.BacklogItemUnit, projectUId);
            backlogItemHoursTA.FillVisibleByProjectUId(project.BacklogItemHours, projectUId);

            projectTA.FillByProjectUId(project.Project, projectUId);
            sprintTA.FillByProjectUId(project.Sprint, projectUId);
             **/

            project.Project.Columns.Add("CurrentSprintUId", typeof(string));
            Data.ProjectDataSet.SprintRow currentSprint = project.Project[0].CurrentSprint;
            if (currentSprint != null)
                project.Project[0]["CurrentSprintUId"] = currentSprint.SprintUId;

            project.Sprint.Columns.Add("ExpirationDateString", typeof(string));
            project.Sprint.Columns.Add("StartDateString", typeof(string));
            project.Sprint.Columns.Add("ApprovalDateString", typeof(string));
            foreach (Data.ProjectDataSet.SprintRow r in project.Sprint) {
                r["StartDateString"] = MiniShortDateString(r.StartDate);
                if (!r.IsExpirationDateNull())
                    r["ExpirationDateString"] = MiniShortDateString(r.ExpirationDate);
                if (!r.IsApprovalDateNull())
                    r["ApprovalDateString"] = MiniShortDateString(r.ApprovalDate);

            }

            reportChildTA.FillByPaymentProjectUId(report.ProjectHourBankChildProjects, ProjectUId);

            report.Merge(project);
            return report;
        }


        public override System.Data.DataSet AdjustDataSet(System.Data.DataSet report) {

            base.AdjustDataSet(report);

            FormatDateTimeFields((Data.ProjectHourBankDataSet)report);
            
            report.Tables["Project"].Columns.Add("HourBankEmmitionDateString", typeof(string));
            report.Tables["Project"].Rows[0]["HourBankEmmitionDateString"] = DateTime.Now.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString();

            report.Tables["Project"].Columns.Add("HourBankFinishDate", typeof(string));            
            report.Tables["Project"].Rows[0]["HourBankFinishDate"] = ((DateTime)report.Tables["Project"].Rows[0]["EstimatedEndDate"]).ToShortDateString();

            short actualBacklogItemNumber = Int16.Parse(report.Tables["BacklogItem"].Compute("max(BacklogItemNumber)", "").ToString());
            System.Data.DataRow[] rs = report.Tables["BacklogItem"].Select("HourBankMonth=" + DateTime.Today.Month + " and HourBankYear=" + DateTime.Today.Year);
            if (rs != null && rs.Length > 0)
                actualBacklogItemNumber = (short)rs[0]["BacklogItemNumber"];

            report.Tables["Project"].Columns.Add("ActualBacklogItemNumber", typeof(string));
            report.Tables["Project"].Rows[0]["ActualBacklogItemNumber"] = actualBacklogItemNumber;

            

            return report;

        }


        private void FormatDateTimeFields(Data.ProjectHourBankDataSet report) {

            report.PayableTasks.Columns.Add("StartString", typeof(string));
            report.PayableTasks.Columns.Add("FinishString", typeof(string));
            foreach (Data.ProjectHourBankDataSet.PayableTasksRow r in report.PayableTasks) {
                if (!r.IsStartNull())
                    r["StartString"] = MiniShortDateString(r.Start);
                if (!r.IsFinishNull())
                    r["FinishString"] = MiniShortDateString(r.Finish);
            }

            report.ProjectHourBankChildProjects.Columns.Add("StartDateString", typeof(string));
            report.ProjectHourBankChildProjects.Columns.Add("EndDateString", typeof(string));
            foreach (Data.ProjectHourBankDataSet.ProjectHourBankChildProjectsRow r in report.ProjectHourBankChildProjects)
            {
                if (!r.IsStartDateNull())
                    r["StartDateString"] = MiniShortDateString(r.StartDate);
                if (!r.IsEndDateNull())
                    r["EndDateString"] = MiniShortDateString(r.EndDate);
            }


        }
    }
}
